package com.zcxy.fast.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxy.fast.bean.PageQuery;
import com.zcxy.fast.entity.ChildTable;
import com.zcxy.fast.vo.ChildTableVo;
import com.zcxy.fast.service.IChildTableService;
import com.zcxy.fast.mapper.ChildTableMapper;
import com.zcxy.fast.mybatisExtend.BaseServiceImpl;
import com.zcxy.fast.utils.BeanCopyUtil;
import com.zcxy.fast.utils.StringUtils;
import com.zcxy.fast.mybatisExtend.LambdaValiteCondition;
import com.zcxy.fast.mybatisExtend.RelationHelper;
import java.util.stream.Collectors;
import cn.hutool.core.collection.CollectionUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;



/**
 * 附表业务层处理
 *
 * @author zcc
 * @email  860474958@qq.com
 * @date   2021-01-15 14:00:36
 */
@Service
@Slf4j
public class ChildTableServiceImpl extends BaseServiceImpl<ChildTableMapper, ChildTable> implements IChildTableService {

    @Override
    public IPage<ChildTableVo> queryPageList(PageQuery<ChildTable> queryVo) {
        ChildTable childTable=queryVo.getT() == null ? new ChildTable() : queryVo.getT();
        QueryWrapper queryWrapper = new QueryWrapper(childTable);
        Page page = new Page(queryVo.getPageNo(), queryVo.getPageSize());
            if(StringUtils.isNotEmpty(childTable.getName())){
            queryWrapper.like("name",childTable.getName());
        }
            if(StringUtils.isNotEmpty(childTable.getCode())){
            queryWrapper.like("code",childTable.getCode());
        }


        IPage<ChildTable> iPage = baseMapper.selectPage(page, queryWrapper);
        return BeanCopyUtil.copyPage(iPage, ChildTableVo.class);
    }

    @Override
    public List<ChildTableVo> queryList(ChildTable childTable) {
        ChildTable childTableq=childTable == null ? new ChildTable() : childTable;
        QueryWrapper queryWrapper = new QueryWrapper(childTableq);
        List<ChildTableVo> ChildTableVos=BeanCopyUtil.copyListToList(baseMapper.selectList(queryWrapper),ChildTableVo.class);
        return ChildTableVos;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addChildTable(ChildTable childTable) {
        LambdaValiteCondition<ChildTable> valiteCondition = LambdaValiteCondition.newInstance(childTable);
        valiteCondition.clear();
        valiteCondition.addField(ChildTable::getCode,ChildTable::getDictId).addErrInfo("编码,字典id 不能重复");
        validFiledExist(valiteCondition);
        baseMapper.insert(childTable);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateChildTable(ChildTable childTable) {
        LambdaValiteCondition<ChildTable> valiteCondition = LambdaValiteCondition.newInstance(childTable);
        valiteCondition.clear();
        valiteCondition.addField(ChildTable::getCode,ChildTable::getDictId).addErrInfo("编码,字典id 不能重复");
        valiteCondition.addExcludes(ChildTable::getId);
        validFiledExist(valiteCondition);


        baseMapper.updateById(childTable);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeAllChildTable(List<String> ids) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("id", ids);
        baseMapper.delete(queryWrapper);
    }
}
